package com.tvunetworks.device.extra.util;


import com.tvunetworks.center.common.model.CommandParam;
import com.tvunetworks.center.common.util.JsonUtil;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.device.extra.exception.ShareException;
import com.tvunetworks.device.extra.model.ReceiverActionResult;
import com.tvunetworks.device.extra.service.feign.RwsFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import static com.tvunetworks.device.extra.model.ReceiverActionResult.SUCCESS;

/**
 * 向 R 发送命令并解析结果
 * <p>
 * error code: 820002
 * @author andy
 **/
@Component
@Slf4j
public class CommandUtil {

    @Autowired
    private RwsFeignService rwsFeignService;
    /**
     * 向 R 执行 2xx 指令
     * @param commandParam p
     * @param clazz c
     * @param <T> t
     * @return r
     */
    public <T extends ReceiverActionResult> T actionCommand(CommandParam commandParam, Class<T> clazz) {
        Result<String> commandResult = rwsFeignService.command(commandParam);
        if (commandResult == null) {
            log.error("Connection R failed, commandResult is empty rid [{}]", commandParam.getRid());
            throw new ShareException("82000103", "Can't get any message from R " + commandParam.getRid());
        }
        if (log.isDebugEnabled()) {
            log.debug("R id [{}] , categoryId [{}] , operationType [{}], result -> [{}]",
                    commandParam.getRid(),
                    commandParam.getCategoryId(),
                    commandParam.getOperationType(),
                    commandResult.getResult());
        }

        if (SUCCESS.equals(commandResult.getErrorCode())) {
            String result = commandResult.getResult();
            if (result.startsWith("[")) {
                return (T) new ReceiverActionResult()
                        .setErrorCode(SUCCESS)
                        .setErrorInfo(result);
            }
            T receiverResult = null;
            try {
                receiverResult = JsonUtil.toObject(commandResult.getResult(), clazz);
            } catch (IOException e) {
                if (!commandResult.getResult().equals("Success")) {
                    log.error("toObject failed [{}],clz [{}]", commandResult.getResult(), clazz);
                    throw new ShareException("82000205", "Parse command result failed :" + commandResult.getResult());
                } else {
                    return (T) new ReceiverActionResult()
                            .setErrorCode(SUCCESS)
                            .setErrorInfo(result);
                }
            }
            if (!receiverResult.assertSuccess()) {
                throw new ShareException("82000299", receiverResult.getErrorInfo());
            }
            return receiverResult;
        }
        log.error("rws return:{}",commandResult);
        // R 链接超时或者 R 没有链接等异常
        return (T) new ReceiverActionResult().setErrorCode(commandResult.getErrorCode()).setErrorInfo(commandResult.getErrorInfo());
    }

    /**
     *
     * @param rid r
     * @param categoryId c
     * @param operationType o
     * @param data d
     * @param origin o
     * @return r
     */
    public ReceiverActionResult actionCommand(String rid, String categoryId, String operationType, String data,String origin) {
        return actionCommand(new CommandParam(rid, categoryId, operationType,null ,data,origin), ReceiverActionResult.class);
    }


}
